﻿using AutoMapper;
using PagedList;
using SIG.Infrastructure.Configs;
using SIG.Model.Articles;
using SIG.Service;
using SIG.ViewModel.Front.Articles;
using SIG.WebMVC.Filters;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace SIG.WebMVC.Controllers
{
    [RoutePrefix("news")]
    public class ArticleController : BaseController
    {

        private readonly IArticleCategoryService _categoryService;
        private readonly IArticleService _articleService;
        private readonly IPageMetaService _pageMetaService;
        private readonly IMapper _mapper;

        public ArticleController(IArticleCategoryService categoryService, IArticleService articleService, IPageMetaService pageMetaService, IMapper mapper)
        {
            _categoryService = categoryService;
            _articleService = articleService;
            _pageMetaService = pageMetaService;
            _mapper = mapper;

        }

        // GET: Article
        [Route]
        [SIGActionFilter]
        public ActionResult Index(int? page, int? categoryId, string keyword)
        {
            var articleListVM = new ArticleListVM();

            if (categoryId != null)
                articleListVM.CategoryId = (int)categoryId;

            articleListVM.Keyword = keyword;
            articleListVM.PageIndex = (page ?? 1) - 1;
            articleListVM.PageSize = SettingsManager.Article.PageSize;
            int totalCount;
            var goodslist = _articleService.GetPagedActiveElements(articleListVM.PageIndex, articleListVM.PageSize, articleListVM.Keyword, articleListVM.CategoryId, out totalCount);
            var categoryVMList = _mapper.Map<List<Article>, List<ArticleVM>>(goodslist);
            articleListVM.TotalCount = totalCount;

            articleListVM.Articles = new StaticPagedList<ArticleVM>(categoryVMList, articleListVM.PageIndex + 1, articleListVM.PageSize, articleListVM.TotalCount); ;

            var categoryList = _categoryService.GetAll().Where(m => m.Active).OrderByDescending(c => c.Importance).ToList();
            var categories = new SelectList(categoryList, "Id", "Title");
            ViewBag.Categories = categories;

            return View(articleListVM);

        }

        [HttpGet]
        [Route("list-{seoname}")]
        public ActionResult ListByCategory(string seoname)
        {
            var pageIndex = 0;
            int pageSize = SettingsManager.Article.PageSize;
            int totalCount;
            var category = _categoryService.GetCategoryBySeoName(seoname);
            var articleList = _articleService.GetPagedActiveElements(pageIndex, pageSize, string.Empty, category.Id, out totalCount);
            var vm = _mapper.Map<List<Article>, List<ArticleVM>>(articleList);

            return View(vm);

        }


        public PartialViewResult LatesterArticles(string seoname, int count)
        {
            var category = _categoryService.GetCategoryBySeoName(seoname);
            var articlelist = _articleService.LatesterArticles(count, category.Id).ToList();
            var vm = _mapper.Map<List<Article>, List<ArticleVM>>(articlelist);

            return PartialView("_LatesterArticles", vm);
        }


        [HttpGet]

        public ActionResult Detail(int id)
        {
            var article = _articleService.GetArticleDetail(id);
            article.ViewCount++;
            _articleService.Update(article);
            return View(article);
        }

        public PartialViewResult ShowArticlesInHome()
        {
            var list = _articleService.RecommendArticles(4);
            var articleList = _mapper.Map<IEnumerable<Article>, IEnumerable<ArticleVM>>(list);
            return PartialView("_ShowArticlesInHome", articleList);
        }
    }
}